<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class field extends check{
	//字段列表
	public function lis(){
		if(!$modelid = intval($this->spArgs('modelid')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		$this->modelid = $modelid;
		$this->field = spClass('m_field')->spLinker()->findAll(array('modelid'=>$modelid),'odnum ASC,fieldid ASC');
		$this->display('field_lis.tpl.php');
	}
	//添加字段
	public function add(){
		if(!$modelid = intval($this->spArgs('modelid')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		$this->modelid = $modelid;
		$modelinfo = spClass('m_table')->find(array('modelid'=>$modelid));
		$this->tablename = $modelinfo['tablename'];
		$this->table = spClass('m_table')->findAll();
		$this->type = spClass('m_type')->findAll();
		$this->display('field_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_field')->spVerifier($this->spArgs())){
			$this->error('数据验证错误！',spUrl('field','add',array('modelid'=>$this->spArgs('modelid'))));
		}
		if(1 == spClass('m_field')->check_field(trim($this->spArgs('field')),trim($this->spArgs('tablename')))){
			$this->error('字段已存在！',spUrl('field','add',array('modelid'=>$this->spArgs('modelid'))));
		}
		$fieldrow = $this->spArgs();
		$fieldrow['canupd'] = 1;
		if(false != spClass('m_field')->create($fieldrow)){
			$typerow = spClass('m_type')->find(array('typeid'=>$fieldrow['typeid']));
			$sql = $typerow['sqladd'];
			$tablerow = spClass('m_table')->find(array('modelid'=>$fieldrow['modelid']));
			$tablename = $GLOBALS['G_SP']['db']['prefix'] . $tablerow['tablename'];
			$fieldname = trim($fieldrow['field']);
			$sql = str_replace('{tablename}', $tablename, $sql);
			$sql = str_replace('{fieldname}', $fieldname, $sql);
			//添加自定义模型表字段
			spDB($tablename,'id')->query($sql);
			$this->success('添加字段成功！',spUrl('field','lis',array('modelid'=>$this->spArgs('modelid'))));
		}else{
			$this->error('添加字段失败！',spUrl('field','lis',array('modelid'=>$this->spArgs('modelid'))));
		}
	}
	//更新字段
	public function upd(){
		if(!$fieldid = intval($this->spArgs('fieldid')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		$fieldinfo = spClass('m_field')->find(array('fieldid'=>$fieldid));
		if($fieldinfo['canupd'] == 0)	$this->error('系统字段禁止操作！',spUrl('field','lis',array('modelid'=>$fieldinfo['modelid'])));
		$modelinfo = spClass('m_table')->find(array('modelid'=>$fieldinfo['modelid']));
		$this->tablename = $modelinfo['tablename'];
		$this->table = spClass('m_table')->findAll();
		$this->type = spClass('m_type')->findAll();
		$this->field = $fieldinfo;
		$this->display('field_upd.tpl.php');
	}
	public function doupd(){
		if(!$fieldid = intval($this->spArgs('fieldid')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		$fieldinfo = spClass('m_field')->find(array('fieldid'=>$fieldid));
		$modelid = $fieldinfo['modelid'];
		if($fieldinfo['canupd'] == 0)	$this->error('系统字段禁止操作！',spUrl('field','lis',array('modelid'=>$modelid)));
		if((1 == spClass('m_field')->check_field(trim($this->spArgs('field')),$this->spArgs('tablename'))) && (trim($this->spArgs('field') != $fieldinfo['field']))){
			$this->error('字段已存在！',spUrl('field','upd',array('fieldid'=>$fieldid)));
		}
		if(false != spClass('m_field')->update(array('fieldid'=>$fieldid),$this->spArgs())){
			$typerow = spClass('m_type')->find(array('typeid'=>$fieldinfo['typeid']));
			$sql = $typerow['sqlupd'];
			$tablerow = spClass('m_table')->find(array('modelid'=>$fieldinfo['modelid']));
			$tablename = $GLOBALS['G_SP']['db']['prefix'] . $tablerow['tablename'];
			$fieldname = $fieldinfo['field'];
			$newfieldname = trim($this->spArgs('field'));
			$sql = str_replace('{tablename}', $tablename, $sql);
			$sql = str_replace('{fieldname}', $fieldname, $sql);
			$sql = str_replace('{newfieldname}', $newfieldname, $sql);
			//更新自定义模型表字段
			spDB($tablename,'id')->query($sql);
			$this->success('更新字段成功！',spUrl('field','lis',array('modelid'=>$modelid)));
		}else{
			$this->success('更新字段失败！',spUrl('field','lis',array('modelid'=>$modelid)));
		}
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		$modelid = $this->spArgs('modelid');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_field')->update(array('fieldid'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('field','lis',array('modelid'=>$modelid)));
	}
	//删除字段
	public function del(){
		if(!$fieldid = intval($this->spArgs('fieldid')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		$fieldinfo = spClass('m_field')->find(array('fieldid'=>$fieldid));
		if($fieldinfo['canupd'] == 0)	$this->error('系统字段禁止操作！',spUrl('field','lis',array('modelid'=>$fieldinfo['modelid'])));
		if(false != spClass('m_field')->delete(array('fieldid'=>$fieldid))){
			$typerow = spClass('m_type')->find(array('typeid'=>$fieldinfo['typeid']));
			$sql = $typerow['sqldel'];
			$tablerow = spClass('m_table')->find(array('modelid'=>$fieldinfo['modelid']));
			$tablename = $GLOBALS['G_SP']['db']['prefix'] . $tablerow['tablename'];
			$fieldname = $fieldinfo['field'];
			$sql = str_replace('{tablename}', $tablename, $sql);
			$sql = str_replace('{fieldname}', $fieldname, $sql);
			//删除自定义模型表字段
			spDB($tablename,'id')->query($sql);
			$this->success('删除字段成功！',spUrl('field','lis',array('modelid'=>$fieldinfo['modelid'])));
		}else{
			$this->error('删除字段失败！',spUrl('field','lis',array('modelid'=>$fieldinfo['modelid'])));
		}
	}
}
